<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <!-- 对树状结构的遍历 （几乎）非递归不可 -->
    <!-- <script>
      var obj = {
        name: "凡哥",
        age: 18,
        wife: {
          name: "如花",
          age: 80,
          firiend: {
            firstName: "西门",
            nickName: "大官人",
            // friends:[
            //     "小潘",
            //     "金莲",
            // ]
            friends: {
              0: "小潘",
              1: "金莲",
            },
          },
        },
      };

      /* 遍历对象的所有层级的节点 */
      /* deep a.很深的 depth n.深度 */
      function printTree(obj, depth = 0) {
        for (var key in obj) {
          var value = obj[key];

          if (typeof value === "object") {
            //   递归打印子树
            printTree(value, depth + 1);
          }

          // value是基本数据类型（叶子结点）
          else {
            /* |--|--|-- */
            var textIndent = "";
            for (var i = 0; i < depth; i++) {
              textIndent += "  |--";
            }
            console.log(textIndent, key, value);
          }
        }
      }

      printTree(obj);
    </script> -->

    <script>
      /* 打仗：丢入一个匹夫 匹夫自己上 丢入一个军官，它麾下的整棵树一起上 */
      var tyson = { name: "mike tyson" };
      var baiqi = {
        name: "白起",
        soldiers: [
          {
            name: "王翦",
            soldiers: [{ name: "王贲" }],
          },
          {
            name: "蒙敖",
            soldiers: [{ name: "蒙恬" }],
          },
        ],
      };
      function war(p) {
        console.log("%s拎着砖头上去了", p.name);

        // 如果它有兵 则所有的兵也一起上
        if (p.soldiers) {
          p.soldiers.forEach(
              function(s){
                war(s)
              }
          )
        }
      }

      war(tyson)
      war(baiqi)
    </script>
  </body>
</html>
